/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: managerSystem.c
> Author: KAIJIE.JIANG
> Mail: 
> Created Time: 2024年07月18日 星期四 18时48分53秒
***************************************************/

#include "managerSystem.h"


//管理员系统函数
void managerSystem(void){
	//管理员登录信息变量
	ML *head_ml = NULL;
	FILE *fp_ml = NULL;
	
	int model = 0;
	int n = 0;

	logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model info]:%s",\
			__FILE__,__FUNCTION__,__LINE__,strerror(errno),"管理员系统");
	system("clear");
	while(1){
		printf("---管理员系统---\n");
		printf("1--管理员注册\n");
		printf("2--管理员登录\n");
		printf("3--管理员修改密码\n");
		printf("0--退出\n");

		printf("model=");
		scanf("%d",&model);

		if(model == 0){
			logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
					__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"0--退出");
			system("clear");
			break;
		}
		switch(model){
			//管理员注册
			case 1:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"1--管理员注册");
				registerManager(fp_ml,head_ml);
				break;
			//管理员登录
			case 2:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"2--管理员登录");
				n = loginManager(fp_ml,head_ml);
				if(n == 1){
					logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
							__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"管理员操作系统");
					managerOperateSystem();
				}
				break;
			//管理员修改密码
			case 3:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"3--管理员修改密码");
				amendManager(fp_ml,head_ml);
				break;
			default:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"输入错误！");
				printf("输入错误！\n");
				break;
		}
	}
}

//管理员账号注册函数//注册时需输入密码登录，密码：1
void registerManager(FILE *fp,ML *head){
	fp = fopen("./managerLogin.txt","w+");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}
	head = createManagerLoginHead();
	head = readManagerLoginFileToLink(fp,head);
	fclose(fp);

//	outputManagerLoginLink(head);
	
	head = addManagerLoginLink(head);
	fp = fopen("./managerLogin.txt","w");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}

	fp = saveManagerLoginLinkToFile(fp,head);

	fclose(fp);
	freeManagerLoginLink(head);
}

//管理员登录函数
int loginManager(FILE *fp,ML *head){
	int n = 0;
	fp = fopen("./managerLogin.txt","r");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}
	head = createManagerLoginHead();
	head = readManagerLoginFileToLink(fp,head);
	n = loginManagerSystem(head);

	freeManagerLoginLink(head);
	fclose(fp);
	printf("登录完成！\n");
	return n;
}

//管理员修改密码函数//修改时需输入密码登录，密码：1
void amendManager(FILE *fp,ML *head){
	fp = fopen("./managerLogin.txt","r");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}
	head = createManagerLoginHead();
	head = readManagerLoginFileToLink(fp,head);
	fclose(fp);
	head = amendManagerLoginLink(head);
	fp = fopen("./managerLogin.txt","w");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}
	fp = saveManagerLoginLinkToFile(fp,head);
	freeManagerLoginLink(head);
	fclose(fp);
	printf("管理员修改密码成功！\n");
}
